package com.metaera.app.ui.activities;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.HotTopicListPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.ChoiceTopicAdapter;
import com.metaera.app.ui.adapters.HotTopicListAdapter;
import com.metaera.app.ui.core.BaseRequestActivity;
import com.metaera.app.ui.popup.SharePopup;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ViewUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 精选专题
 */
public class ChoiceTopicActivity extends BaseRequestActivity {

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_choice_topic;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_container));

        // find view
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.smart_refresh_layout);
        RecyclerView recyclerChoiceTopic = findViewById(R.id.recycler_choice_topic);
        refreshLayout.setEnableRefresh(false);

        // init view
        ActivityTitleHelper.setTitle(this, R.string.home_topic);
        recyclerView.setAdapter(new HotTopicListAdapter());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(recyclerView.getContext(), R.drawable.shape_item_divider);
        dividerItemDecoration.setDrawable(drawable);
        recyclerView.addItemDecoration(dividerItemDecoration);

        // network
        HotTopicListPresenter presenter = new HotTopicListPresenter();
        presenter.setPaginationView(new BasicPaginationView<>(recyclerView, refreshLayout, this));
        presenter.callback(data -> recyclerChoiceTopic.setAdapter(new ChoiceTopicAdapter(data)));
        presenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(presenter));
        findViewById(R.id.iv_share).setOnClickListener(v -> {

            new XPopup.Builder(v.getContext())
                    .asCustom(
                            new SharePopup(
                                    v.getContext(),
                                    "Meta Era专题：关注行业趋势",
                                    "行业热点，我们帮你做了汇总",
                                    ApiParam.LINK_SHARE_4,
                                    R.mipmap.ic_launcher
                            )
                    )
                    .show();

        });
    }

}
